var path = require('path');
var logger = require('morgan');
var express = require('express');
var favicon = require('serve-favicon');
var bodyParser = require('body-parser');

var app = express();
var serverRender = require('./serverRender');

// 网站logo favicon
app.use(favicon(path.join(__dirname, '../public', 'favicon.ico')));
app.use(logger('tiny'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, '../public')));
app.use(serverRender);

// 错误处理
app.use(function(err, req, res, next) {
  // 只在开发时提供错误信息
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // 渲染错误500
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;
